package cs575.wido.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;

import cs575.wido.action.AddTaskAction;
import cs575.wido.action.EditTaskAction;
import cs575.wido.action.NewTaskAction;
import javax.swing.JComboBox;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.Action;

public class TaskDetailsWindow extends JDialog {
	private cs575.client.Task task;
	private Action action;
	
	private JButton btnCancel;
	private JButton btnOk;
	
	private JTextField txtName;
	private JComboBox comboBoxStatus;
	private JTextField txtDuedate;
	private JTextArea txtDescription;
	
	private static String[] statusList = {"Active", "Complete"};

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			//TaskDetailsWindow dialog = new TaskDetailsWindow();
			//dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			//dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public TaskDetailsWindow(cs575.client.Task task) {
		this.task = task;
		
		setTitle("Task Details");
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		getContentPane().add(buildFormPanel(), BorderLayout.CENTER);
		getContentPane().add(buildButtonPane(), BorderLayout.SOUTH);
	}
	
	
	
	private JComponent buildFormPanel() {
		JLabel lblName;
		JLabel lblStatus;
		JLabel lblDueDate;
		JScrollPane scrollPane;
		JLabel lblDescription;
		
		JPanel formPanel = new JPanel();
		formPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(formPanel, BorderLayout.CENTER);
		formPanel.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),},
			new RowSpec[] {
				FormFactory.MIN_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),}));
		
		lblName = new JLabel("Name");
		formPanel.add(lblName, "2, 2, right, default");
		
		txtName = new JTextField();
		formPanel.add(txtName, "4, 2, fill, default");
		txtName.setColumns(10);
		
		lblStatus = new JLabel("Status");
		formPanel.add(lblStatus, "2, 4, right, default");
		
		comboBoxStatus = new JComboBox(statusList);
		formPanel.add(comboBoxStatus, "4, 4, fill, default");
		
		lblDueDate = new JLabel("Due Date");
		formPanel.add(lblDueDate, "2, 6, right, default");
		
		txtDuedate = new JTextField();
		formPanel.add(txtDuedate, "4, 6, fill, default");
		txtDuedate.setColumns(10);
		
		lblDescription = new JLabel("Description");
		formPanel.add(lblDescription, "2, 8");
		
		txtDescription = new JTextArea();
		
		scrollPane = new JScrollPane(txtDescription);
		formPanel.add(scrollPane, "4, 8, fill, fill");
		
		return formPanel;
	}
	
	private JComponent buildButtonPane() {
		JPanel buttonPane = new JPanel();
		FlowLayout flowLayout = (FlowLayout) buttonPane.getLayout();
		flowLayout.setAlignment(FlowLayout.RIGHT);
		
		btnOk = new JButton("OK");
		buttonPane.add(btnOk);
		getRootPane().setDefaultButton(btnOk);
		
		btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				TaskDetailsWindow.this.dispose();
			}
		});
		buttonPane.add(btnCancel);
		
		buildActions();
		
		return buttonPane;
	}
	
	private void buildActions() {
		btnOk.setAction(this.getAction());
	}
	
	public String getName() {
		return txtName.getText();
	}
	
	/**
	 * 
	 * @return active 1; complete 2
	 */
	public int getStatus() {
		return comboBoxStatus.getSelectedIndex() + 1;
	}
	
	public String getDueDate() {
		return txtDuedate.getText();
	}
	
	public String getDescription() {
		return txtDescription.getText();
	}

	public Action getAction() {
		return action;
	}

	public void setAction(Action action) {
		this.action = action;
		this.buildActions();
		
		if (action instanceof EditTaskAction) {
			this.setFields();
		}
	}
	
	private void setFields() {
		String name = this.task.getTaskName();
		int status = this.task.getStatus();
		String dueDate = this.task.getDueDate();
		String description = this.task.getTaskDescription();
		
		txtName.setText(name);
		txtDuedate.setText(dueDate);
		txtDescription.setText(description);
		comboBoxStatus.setSelectedIndex(status - 1);
	}
	
	public cs575.client.Task getTask() {
		return this.task;
	}

}
